home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / loading / BldSymX.c < prev    next >
C/C++ Source or Header  |  1990-10-03  |  6KB  |  216 lines

  1. /* begincopyright
  2.   Copyright (c) 1988 Xerox Corporation. All rights reserved.
  3.   Use and copying of this software and preparation of derivative works based
  4.   upon this software are permitted. Any distribution of this software or
  5.   derivative works must comply with all applicable United States export
  6.   control laws. This software is made available AS IS, and Xerox Corporation
  7.   makes no warranty about the software, its performance or its conformity to
  8.   any specification. Any person obtaining a copy of this software is requested
  9.   to send their name and post office or electronic mail address to:
  10.     PCR Coordinator
  11.     Xerox PARC
  12.     3333 Coyote Hill Rd.
  13.     Palo Alto, CA 94304
  14.   endcopyright */
  15.  
  16. /*
  17.  * BldSymX.c
  18.  *
  19.  * Demers, March 9, 1990 9:22:54 am PST
  20.  * Aizawa January 5, 1990 2:54:54 pm PST
  21.  */
  22.  
  23. #include "xr/BasicTypes.h"
  24. #include "xr/IncrementalLoad.h"
  25. #include "xr/BldSymX.h"
  26.  
  27. /*
  28.  * Internal codes -- not client-visible
  29.  */
  30.  
  31. #define N_IGNORE    0x10
  32. #define N_HEADSTAB    0x12
  33. #define N_PCSTAB    0x14
  34. #define N_BODYSTAB    0x16
  35.  
  36. #define STRINGSX_OFF 1    /* 0 is not a valid index */
  37.  
  38.  
  39. /*
  40.  * Kludge -- embedded file names created with identifiable prefix
  41.  *   (keeps `ld' from discarding file names that start with 'L')
  42.  */
  43.  
  44. static char *fileNamePrefix = "FILE-";    /* syntax is "FILE-31416-foo.o" */
  45.  
  46. static char *
  47. NameAsFileName(name)
  48.     char *name;
  49. {
  50.     char *np, *pp;
  51.     int len;
  52.  
  53.     if( name == NIL )
  54.         return NIL;
  55.  
  56.     np = name;
  57.     pp = fileNamePrefix;
  58.     for(;;) {
  59.         if( *pp == 0 ) {
  60.             name = np;
  61.             while( (*np >= '0') && (*np <= '9') ) np++;
  62.             return ( (*np == '-') ? (np+1) : name );
  63.         }
  64.         if( *np++ != *pp++ ) break;
  65.     }
  66.  
  67.     if( (len = strlen(name)) < 2 )
  68.         return NIL;
  69.     if( strcmp((name+len-2), ".o") != 0 )
  70.         return NIL;
  71.     return name;
  72. }
  73.  
  74.  
  75.  
  76.  
  77. char *
  78. XR_BldSymX(sxd, allocProc, clientData, staticTextOnly)
  79.     XR_SymXData sxd;
  80.     char *((*allocProc)(/*void *clientData, XR_SymXData sxd*/));
  81.     char *clientData;
  82.     bool staticTextOnly;
  83. {
  84.     struct nlist *p;
  85.     struct nlist *recentStab;
  86.     struct nlist *headStab;
  87.     int index, bytesLeft;
  88.     struct nlistx *pTo;
  89.     char *sFrom, *sTo;
  90.     struct dbxstabs *stabsTo;
  91.     unsigned stabsHiWat;
  92.     unsigned char pType;
  93.     char *result = NIL;
  94.  
  95.     sxd->sxd_nsymsx = 0;
  96.     sxd->sxd_nstringsx = 0;
  97.     sxd->sxd_nstabs = 0;
  98.  
  99.     recentStab = NIL;
  100.     headStab = NIL;
  101.  
  102.     /*
  103.      * Pass through symbol table
  104.      * Count stabs groups
  105.      * Count symbols we want to keep
  106.      */
  107.  
  108.     for( ((index = 0),(bytesLeft = sxd->sxd_hdr->a_syms),(p = sxd->sxd_syms))
  109.         ; bytesLeft > 0
  110.         ; ((index += 1),(p += 1),(bytesLeft -= (sizeof(*p))))
  111.     ) {
  112.         pType = p->n_type;
  113.         if( /* DBX STAB */ (pType & N_STAB) != 0 ) {
  114.             switch( pType ) {
  115.                 case N_SO:
  116.                     if( (headStab == NIL)
  117.                             || (headStab->n_value != p->n_value)
  118.                             || (p != (recentStab + 1))
  119.                     ) {
  120.                         /* this is the start of a stab group; */
  121.                         sxd->sxd_nstabs += 1;
  122.                         p->n_type = N_HEADSTAB;
  123.                         headStab = p;
  124.                     } else {
  125.                         p->n_type = N_PCSTAB;
  126.                     }
  127.                     break;
  128.                 case N_FUN:
  129.                     p->n_type = N_PCSTAB;
  130.                     break;
  131.                 case N_SLINE:
  132.                     p->n_type = N_PCSTAB;
  133.                     break;
  134.                 default:
  135.                     p->n_type = N_BODYSTAB;
  136.                     break;
  137.             }
  138.             recentStab = p;
  139.         } else if( /* sym to keep */ (pType == N_TEXT)
  140.                 || ((!staticTextOnly) && (pType & N_EXT)) ) {
  141.             sxd->sxd_nsymsx += 1;
  142.             if( p->n_un.n_strx != 0 ) {
  143.                 p->n_un.n_name = sxd->sxd_strings + p->n_un.n_strx;
  144.                 if( pType == N_TEXT ) /* internal text */ {
  145.                     char *nameAsFileName = NameAsFileName(p->n_un.n_name);
  146.                     if( nameAsFileName != NIL ) {
  147.                         p->n_un.n_name = nameAsFileName;
  148.                         p->n_type = N_MODULE;
  149.                     }
  150.                 }
  151.                 sxd->sxd_nstringsx += (1 + strlen(p->n_un.n_name));
  152.             }
  153.         } else /* sym to ignore */ {
  154.             p->n_type = N_IGNORE;
  155.         }
  156.     }
  157.  
  158.     /*
  159.      * Call allocProc to allocate new storage
  160.      */
  161.  
  162.     if( sxd->sxd_nstringsx > 0 ) sxd->sxd_nstringsx += STRINGSX_OFF;
  163.     result = (*allocProc)(clientData, sxd);
  164.     if( result != NIL ) return result;
  165.  
  166.     /*
  167.      * Copy the interesting stuff to the newly-allocated storage
  168.      */
  169.  
  170.     pTo = sxd->sxd_symsx;
  171.     sTo = sxd->sxd_stringsx + STRINGSX_OFF;
  172.     stabsTo = NIL;
  173.  
  174.     for( ((index = 0),(bytesLeft = sxd->sxd_hdr->a_syms),(p = sxd->sxd_syms))
  175.         ; bytesLeft > 0
  176.         ; ((index += 1),(p += 1),(bytesLeft -= (sizeof(*p))))
  177.     ) {
  178.         switch( p->n_type ) {
  179.             case N_IGNORE:
  180.                 break;
  181.             case N_HEADSTAB:
  182.                 stabsTo = ( (stabsTo == NIL)
  183.                         ? sxd->sxd_stabs : (stabsTo + 1) );
  184.                 stabsTo->dbxs_x = index;
  185.                 stabsTo->dbxs_n = 1;
  186.                 stabsTo->dbxs_pcLow = stabsTo->dbxs_pcHigh = p->n_value;
  187.                 break;
  188.             case N_PCSTAB:
  189.                 if( stabsTo == NIL ) /* can't happen? */ {
  190.                     return "a.out format error: first DBX STAB not SO";
  191.                 }
  192.                 if( stabsTo->dbxs_pcHigh < p->n_value )
  193.                     stabsTo->dbxs_pcHigh = p->n_value;
  194.                 /* fall thru */
  195.             case N_BODYSTAB:
  196.                 if( stabsTo == NIL ) /* can't happen? */ {
  197.                     return "a.out format error: first DBX STAB not SO";
  198.                 }
  199.                 stabsTo->dbxs_n = (index + 1 - stabsTo->dbxs_x);
  200.                 break;
  201.             default:
  202.                 bcopy(p, pTo, sizeof(struct nlist));
  203.                 pTo->nx_x = index;
  204.                 if( (sFrom = p->n_un.n_name) != NIL ) {
  205.                     pTo->nx_n.n_un.n_strx = (sTo - sxd->sxd_stringsx);
  206.                     while( (*sTo++ = *sFrom++) != 0 ) continue;
  207.                 }
  208.                 pTo += 1;
  209.                 break;
  210.         }
  211.     }
  212.  
  213.     return NIL;
  214. }
  215.  
  216.